package com.anycc.cloud.core.entity;

import com.anycc.cloud.common.entity.BaseEntity;
import com.anycc.cloud.core.enums.Status;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 账户
 */
@Entity
@Table(
        name = "sys_account",
        indexes = {@Index(
                name = "i_sys_account_user_name",
                columnList = "user_name"
        ), @Index(
                name = "i_sys_account_status",
                columnList = "status"
        )}
)
public class Account extends BaseEntity {
    private static final long serialVersionUID = -2722269984440465390L;
    public static final String FIELD_USER_NAME = "username";
    public static final String FIELD_PASSWORD = "password";
    public static final String FIELD_NEW_PWD = "newPwd";
    public static final String FIELD_OLD_PWD = "oldPwd";
    public static final String FIELD_CONFIRM_PWD = "confirmPwd";
    public static final String FIELD_SALT = "salt";
    public static final String FIELD_STATUS = "status";
    public static final String FIELD_ORG = "org";
    public static final String FIELD_POSITIONS = "positions";
    public static final String FIELD_ROLES = "roles";
    private String username;
    private String password;
    private Status status;
    private Org org;
    private List<Role> roles = new ArrayList<>();

    public Account() {
    }

    @Column(
            name = "user_name",
            length = 36,
            nullable = false,
            unique = true
    )
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @JsonIgnore
    @Column(
            name = "password",
            length = 128,
            nullable = false
    )
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Enumerated(EnumType.STRING)
    @Column(
            name = "status",
            length = 16,
            nullable = false
    )
    public Status getStatus() {
        return this.status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @ManyToOne
    @JoinColumn(
            name = "org_id"
    )
    @JsonIgnoreProperties({"parent", "children", "updateTime", "createTime"})
    public Org getOrg() {
        return this.org;
    }

    public void setOrg(Org org) {
        this.org = org;
    }


    @ManyToMany
    @JoinTable(
            name = "sys_account_role",
            joinColumns = {@JoinColumn(
                    name = "account_id",
                    referencedColumnName = "id"
            )},
            inverseJoinColumns = {@JoinColumn(
                    name = "role_id",
                    referencedColumnName = "id"
            )}
    )
    @JsonIgnoreProperties({"permissions", "description", "updateTime", "createTime"})
    public List<Role> getRoles() {
        return this.roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
}